#!/usr/bin/env python
# coding: utf-8

# 更新日期：2020/11/16
# 撰写人：陈珂
# 
# ------
# 
# 
# # 课后练习：
# > * 1、地理/逆地理编码 
# > * 2、路径规划
# > * 3、行政区域查询
# > * 4、搜索POI
# > * 5、IP定位
# > * 6.静态地图
# > * 7.天气查询
# > * 8.输入提示
# 
# 

# ##  地理/逆地理编码   
# 地理编码/逆地理编码 API 是通过 HTTP/HTTPS 协议访问远程服务的接口，提供结构化地址与经纬度之间的相互转化的能力。
# 结构化地址的定义： 首先，地址肯定是一串字符，内含国家、省份、城市、区县、城镇、乡村、街道、门牌号码、屋邨、大厦等建筑物名称。按照由大区域名称到小区域名称组合在一起的字符。一个有效的地址应该是独一无二的。注意：针对大陆、港、澳地区的地理编码转换时可以将国家信息选择性的忽略，但省、市、城镇等级别的地址构成是不能忽略的。暂时不支持返回台湾省的详细地址信息。
# 

# ### 准备工作

# In[1]:


import requests,json
import pandas as pd
from PIL import Image
from io import BytesIO
chen_key = "7273698e694b1e8e9ad6de749cdb6392"


# In[2]:


# 准备数据
中大南方_address = "广州市从化区温泉镇温泉大道882号中山大学南方学院"


# In[3]:


"""获取地理编码函数"""
"""address为结构化地址"""
def geocode(key,address):
    url = "https://restapi.amap.com/v3/geocode/geo?parameters"
    params = {
        "key":chen_key,
        "address":中大南方_address    
    }
    response =  requests.get(url,params=params)
    results =  pd.json_normalize(response.json()['geocodes'])
    return results #返回值


# In[4]:


geocode(chen_key,中大南方_address)


# ### 逆地理编码

# In[5]:


# 准备数据
location_中大南方 = geocode(chen_key,中大南方_address) #直接调用地理编码的数据


# In[6]:


def Inverse_geocoding(key,location,extensions='all'):
    url = "https://restapi.amap.com/v3/geocode/regeo?parameters"
    params = {
        "key":chen_key,
        "location":location,
        "extensions":extensions,
       
}
    response = requests.get(url=url,params=params)
    results = pd.json_normalize(response.json())
    return results


# In[7]:


Inverse_geocoding(chen_key,location_中大南方)


# ## 路径规划

# ### 步行路径规划

# In[8]:


#准备数据
中大南方_address = "广东省广州市从化区温泉镇温泉大道882号中山大学南方学院"
博学路_origin = "113.68,23.634" #出发点博学路
S29从莞深高速_destination = "113.682,23.6394" #目的地S29从莞深高速


# In[9]:


"""获取步行路径函数"""
"""origin为起始地的坐标，destination为目的地的坐标"""
def walking(key,origin,destination,output='JSON',callback=None):
    url= 'https://restapi.amap.com/v3/direction/walking?parameters'
    params = {
        "key":chen_key,
        "origin":origin,
        "destination":destination,
        "output":output,
        "callback":callback
    }
    response =  requests.get(url=url,params=params)
    results = pd.json_normalize(response.json()['route']['paths'][0]['steps'])
    return results


# In[10]:


walking(chen_key,博学路_origin,S29从莞深高速_destination)


# ### 公交路径规划

# In[11]:


# 准备数据

博学路_origin = "113.68,23.634" #出发点博学路
越秀区_destination = "113.26683,23.12897" #目的地越秀区
广州_city = "020"
extensions="all"
strategy = "0"


# In[12]:


def bus(key,origin,destination,city,extensions = "all",strategy= 0,nightflag=0,output='JSON',callback=None):
    url = "https://restapi.amap.com/v3/direction/transit/integrated?parameters"    
    params = {
        "key":chen_key,
        "origin":origin,
        "destination":destination,
        "city":city,
        "extensions":extensions,
        "strategy":strategy,
        "nightflag":nightflag,
        "output":output,
        "callback":callback
    }
    response =  requests.get(url=url,params=params)
    results = response.json()
    return results


# In[13]:


bus(chen_key,博学路_origin,越秀区_destination,广州_city)


# In[14]:


pd.json_normalize(bus(chen_key,博学路_origin,越秀区_destination,广州_city)['route']['transits'][0][ 'segments'][0]['walking']['steps'])


# ### 驾车路径规划

# In[15]:


# 准备数据
博学路_origin = "113.68,23.634" #出发点博学路
越秀区_destination = "113.26683,23.12897" #目的地越秀区
strategy = "10"
extensions = "all"


# In[16]:


def drive(key,origin,destination,strategy = "10",extensions = "all"):
    url = "https://restapi.amap.com/v3/direction/driving?parameters"
    params = {
    "key":chen_key,
    "origin":origin,
    "destination":destination,
    "strategy":strategy,
    "extensions":extensions
    }
    response =  requests.get(url=url,params=params)
    results =  response.json()
    return results


# In[17]:


drive(chen_key,博学路_origin,越秀区_destination,strategy ,extensions )


# In[18]:


pd.json_normalize(drive(chen_key,博学路_origin,越秀区_destination,strategy ,extensions )['route']['paths'])


# ### 行政区域查询

# In[19]:


# 准备数据
从化区_adcode = "440117"
广州市_citycode ="020"


# In[20]:


def administrative_region(key,adcode = '440117',citycode = '020'):
    url="https://restapi.amap.com/v3/config/district?parameters"
    params = {
        "key":chen_key,
        "adcode":从化区_adcode,
        "citycode":广州市_citycode
    }
    response =  requests.get(url=url,params=params)
    results = response.json()
    return results


# In[21]:


administrative_region(chen_key,从化区_adcode,广州市_citycode)


# In[22]:


pd.json_normalize(administrative_region(chen_key,从化区_adcode,广州市_citycode)['districts'][0]['districts'])


# ### 骑行路径规划

# In[23]:


博学路_origin = "113.68,23.634" #出发点博学路
从化客运站_destination = "113.605763,23.534122" #目的地从化客运站


# In[24]:


def bike(key,origin,destination):
    url="https://restapi.amap.com/v4/direction/bicycling?parameters"
    params ={
    "key":chen_key,
    "origin":origin,
    "destination":destination
    }
    response =  requests.get(url=url,params=params)
    results = response.json()
    return results


# In[25]:


bike(chen_key,博学路_origin,从化客运站_destination)


# In[26]:


pd.json_normalize( bike(chen_key,博学路_origin,从化客运站_destination)['data']['paths'][0]['steps'])


# ## 搜索POI
# 

# In[27]:


# 准备数据
keywords ="美食"
types = "广州"
从化区_city ="从化区"#广州市
children = "1"
page ="1"
offset="20"
extensions = "all"


# In[28]:


def sousuo(key,keywords,types,city,children,offset,page,extensions):
    url ="https://restapi.amap.com/v3/place/text?parameters"
    params = {
        "key":chen_key,
        "keywords":keywords,
        "types":types,
        "city":city,
        "children":children,
        "offset":offset,
        "page":page,
        "extensions":extensions,
    }
    response =  requests.get(url=url,params=params)
    results = response.json()
    return results


# In[29]:


sousuo(chen_key,keywords,types,从化区_city,children,offset,page,extensions)


# ### 周边搜索

# In[30]:


#准备数据
一品皇美食_location ="113.504486,23.451176"#一品皇美食、
keywords = "美食"  
从化区_city = "从化区"
extensions = "all"


# In[31]:


def zhoubian(key,location,keywords,city,extensions):
    url ="https://restapi.amap.com/v3/place/around?parameters"
    params = {
        "key":chen_key,
        "location":location,
        "keywords":keywords,
        "city":city,
        "extensions":extensions
    }
    response =  requests.get(url=url,params=params)
    results = response.json()
    return results


# In[32]:


zhoubian(chen_key,一品皇美食_location ,keywords,从化区_city ,extensions)


# ### 多边形搜索

# In[33]:


# 准备数据
polygon ="116.460988,40.006919|116.48231,40.007381;116.47516,39.99713|116.472596,39.985227|116.45669,39.984989|116.460988,40.006919"#肯德基
keywords =  "肯德基"
types = "050301"
extensions = "all"


# In[34]:


def suobianxing(key,polygon,keywords,types,extensions):
    url ="https://restapi.amap.com/v3/place/polygon?parameters "
    params = {
        "key":chen_key,
        "polygon":polygon,
        "keywords":keywords,
        "types":types,
        "extensions":extensions
    }
    response =  requests.get(url=url,params=params)
    results = response.json()
    return results


# In[35]:


suobianxing(chen_key,polygon,keywords,types,extensions)


# In[36]:


pd.json_normalize(suobianxing(chen_key,polygon,keywords,types,extensions)['pois'])


# ### ID查询

# In[37]:


# 准备数据
id="B0FFFAB6J2"


# In[38]:


def ID(key,id):
    url="https://restapi.amap.com/v3/place/detail?parameters" 
    params = {
        "key":chen_key,
        "id":id
    }
    response =  requests.get(url=url,params=params)
    results = response.json()
    return results


# In[39]:


ID(chen_key,id)


# ##  IP定位

# In[40]:


#准备数据
ip ="114.247.50.2"


# In[41]:


def IP(key,ip):
    url ="https://restapi.amap.com/v3/ip?parameters"
    params = {
        "key":chen_key,
        "id":id
    }
    response =  requests.get(url=url,params=params)
    results = response.json()
    return results


# In[42]:


IP(chen_key,ip)


# In[43]:


pd.json_normalize(IP(chen_key,ip))


# ## 静态地图

# In[44]:


# 准备数据
url ="https://restapi.amap.com/v3/staticmap?parameters"
zoom = "12"
size = "1024*1024"
scale = "2"


# In[45]:


"""获取静态地图函数"""
"""location为中心点坐标，zoom为地图缩放级别:[1,17]"""
def staticmap(key,location,zoom,size="600*300",scale=2,markers=None,labels=None,paths=None,traffic=1):
    #zoom=None,size=400*400,scale=2,markers=None,labels=None,paths=None,traffic=1
    url = "https://restapi.amap.com/v3/staticmap?parameters"
    params = {
        "key":key,
        "location":location,
        "zoom":zoom,
        "size":size,
        "scale":scale,
        "markers":markers,
        "labels":labels,
        "paths":paths,
        "traffic":traffic
    }
    r = requests.get(url,params=params)
    result = r.content
    results = Image.open(BytesIO(r.content))
    return results


# In[46]:


staticmap_location = 博学路_origin


# In[47]:


staticmap(chen_key,staticmap_location,12)


# ## 坐标转换

# In[48]:


"""坐标转换函数"""
"""location为所需转换的地址的坐标"""
def coordinate_convert(key,locations,coordsys="autonavi",output='JSON'):
    url = "https://restapi.amap.com/v3/assistant/coordinate/convert?parameters"
    params = {
        "key":key,
        "locations":locations,
        "coordsys":coordsys,
        "output":output
    }
    response = requests.get(url,params=params)
    result = response.json()
    return result


# In[49]:


coordinate_convert(chen_key,博学路_origin)


# ## 天气查询

# In[50]:


# 准备数据
从化区_city = "440117"
extensions = "all"
output = "json"


# In[51]:


def weather(key,city,extensions="base",output='JSON'):
    url = "https://restapi.amap.com/v3/weather/weatherInfo?parameters"
    params = {
        "key":key,
        "city":city,
        "extensions":extensions,
        "output":output
    }
    response = requests.get(url,params=params)
    results = response.json()
    return results


# In[52]:


weather(chen_key,从化区_city)


# In[53]:


pd.json_normalize(weather(chen_key,从化区_city)['lives'])


# ## 输入提示

# In[57]:


keywords = "肯德基"
location = "116.481488,39.990464"
city = "北京"
datatype = "all"


# In[58]:


"""获取输入提示函数"""
"""keywords为搜索关键字，city为所需查询的城市的城市编码"""
def tishi(key,keywords,city,type=None,location=None,citylimit="true",datatype="all",output='JSON',callback=None):
    url = "https://restapi.amap.com/v3/assistant/inputtips?parameters"
    params = {
        "key":key,
        "keywords":keywords,
        "city":city,
        "type":type,
        "location":location,
        "citylimit":citylimit,
        "datatype":datatype,
        "output":output,
        "callback":callback
    }
    response = requests.get(url,params=params)
    result = pd.json_normalize(response.json()['tips'])
    return result


# In[56]:


tishi(chen_key,keywords,location,city)

